package com.example.goodsserver.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.goodsserver.entity.Collection;
import com.example.goodsserver.manager.CollectionManager;
import com.example.goodsserver.params.CollectionParams;
import com.example.goodsserver.params.PageCollectionParams;
import com.example.goodsserver.vo.CollectionVO;
import com.example.userserver.enums.ReturnEnum;
import com.example.userserver.utils.BeanCopyUtils;
import com.example.userserver.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectionService {
    @Autowired
    private CollectionManager collectionManager;
    public PageInfo<CollectionVO> pageCollection(PageCollectionParams pageCollectionParams){
        pageCollectionParams.setOrderBy("c.time desc");
        PageHelper.startPage(pageCollectionParams);
        List<CollectionVO> list = collectionManager.listCollection(pageCollectionParams.getUserId());
        PageInfo<CollectionVO> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public void deleteCollection(String id) {
        collectionManager.deleteCollection(id);
    }

    public Result insertCollection(CollectionParams collectionParams) {
        Collection collectionExist = collectionManager.getCollectionExist(collectionParams.getUserId(), collectionParams.getGoodsId());
        if(ObjectUtil.isNotEmpty(collectionExist)){
            return new Result(null, ReturnEnum.COLLECTION_RE);
        }
        Collection collection = new Collection();
        BeanCopyUtils.copyPropertiesIgnoreNull(collectionParams, collection);
        collectionManager.insertCollection(collection);
        return new Result(null, ReturnEnum.SUCCESS);
    }

}
